package game.tycoon.sosial;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class WorldActivity extends Activity {

	private static final String TAG = WorldActivity.class.getSimpleName();
	private WorldView worldView;
//	BroadcastReceiver mReceiver;
	DisplayMetrics metrics;
//	boolean isMultiplayer;
	
//	public static final String EXTRA_WINNER_NAME = "WINNER_NAME";
//	public static final String EXTRA_WINNER_GUNDU = "WINNER_GUNDU";	
//	public static final String EXTRA_LOSERS_NAMES = "LOSERS_NAMES";
//	public static final String EXTRA_LOSERS_GUNDU = "LOSERS_GUNDU";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);		
		//baru pertama kali dilaunch(sebelom nya belom dilaunch)
		Log.d(TAG,"start World activity");
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		//instansiasi worldView
		worldView = new WorldView(this,metrics.widthPixels,metrics.heightPixels);
		setContentView(worldView);	
		//nangkep screen off atau on
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
	}
	
	@Override
	public boolean onKeyDown(int KeyCode, KeyEvent event) {				
		if(KeyCode == KeyEvent.KEYCODE_MENU || KeyCode == KeyEvent.KEYCODE_BACK) {
	    	Log.d(TAG,"tombol menu ditekan");
//	    	if(worldView.state == WorldView.PAUSE) {
//	    		worldView.state = WorldView.RUNNING;
//	    		if(!worldView.thread.isAlive()) {//jika game loop sudah gak hidup
//		    		worldView.initThread();
//		    	}
//	    	}else{
//	    		worldView.state = WorldView.PAUSE;	    			    	
//	    	}
	    	exitToGameMode();
	    	return true;
	    }
	    return super.onKeyDown(KeyCode, event);	
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		overridePendingTransition(R.anim.fade, R.anim.hold);
	}
	
	@Override
	protected void onResume() {
		Log.d(TAG,"onResume()");
		/*
		 * ketika screen berubah dari mati ingin ke hidup
		 */
//		if(!ScreenReceiver.wasScreenOn) {//ada perubahan kondisi layar
//			worldView.state = WorldView.PAUSE;
//			worldView.initThread();
//		}
		super.onResume();
		//activity-nya telah visible		
	}	
	
	@Override
	protected void onPause() {
		Log.d(TAG,"onPause()");
		/*
		 * ketika screen mati
		 */			
		worldView.state = WorldView.PAUSE;
		worldView.thread.setRunning(false);//matiin thread
		//activity lain mengambil fokus			
		super.onPause();			
	}
	
	public void exitToGameMode() {		
//		this.startActivity(new Intent("android.intent.action.GAMEMODE"));
//		overridePendingTransition(R.anim.fade, R.anim.hold);				
		worldView.thread.setRunning(false);
//		GameSound.changeMusicFromGameToMain(this);
		this.finish();		
	}
	
	public void onStop() {		
//		unregisterReceiver(mReceiver);	
//		super.onStop();
	}
	
	public void showTutorial(final String... texts) {
		final Context context = this;	
		if(WorldSetting.statusTutorial) {//jika tutorial di enable
			if(texts.length==1) {//jika panjangnya 1
				worldView.post(new Runnable() {
					public void run() {
						AlertDialog.Builder builder = new AlertDialog.Builder(context);
						builder.setMessage(texts[0])
							   .setCancelable(false)
							   .setPositiveButton("Lanjut", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface arg0, int arg1) {								
										arg0.cancel();
									}							
							   	});
						builder.show();//tunjukkan kotak dialog
					}
				});	
			}
			else if(texts.length>=2) {//jika ada dua atau lebih parameter
				final AlertDialog[] arrDialog = new AlertDialog[texts.length];
				int[] arrint = new int[texts.length];
				for(int i=0;i<texts.length;i++) {
					arrint[i] = i;
				}
				/*
				 * loop mulai dari parameter terakhir, parameter terakhir kalo dipencet lanjut menghilangkan dirinya sendiri
				 * kalo bukan parameter terakhir dipencet lanjut akan lanjut ke alertdialog berikutnya
				 */
				for(final int i : arrint) {
					final AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage(texts[texts.length-i-1]).setCancelable(false);				
					if(i!=0) {//jika bukan parameter terakhir
						builder.setPositiveButton("Lanjut", new DialogInterface.OnClickListener() {						
							public void onClick(DialogInterface arg0, int arg1) {														
								worldView.post(new Runnable() {
									public void run() {									
										arrDialog[texts.length-i].show();		
										if (texts[texts.length-i-1].equals(WorldView.H_GARISBAWAH)){
							    			worldView.highlightGame(WorldView.GARIS_BATAS_BAWAH);
										}
										if (texts[texts.length-i-1].equals(WorldView.H_GARISAPOLLO)){
							    			worldView.highlightGame(WorldView.GARIS_ROKET_APOLLO);
										}
										if (texts[texts.length-i-1].equals(WorldView.H_GARISBULETAN)){
							    			worldView.highlightGame(WorldView.GARIS_BULETAN);
										}

									}								
								});																
							}
						});	
						worldView.post(new Runnable() {
							public void run() {
								arrDialog[texts.length-i-1] = builder.create();
							}						
						});									
					}else{//jika parameter terakhir
						builder.setPositiveButton("Lanjut", new DialogInterface.OnClickListener() {						
							public void onClick(DialogInterface arg0, int arg1) {							
								arg0.cancel();
								if (texts[texts.length-i-1].equals(WorldView.H_GARISBAWAH)){
					    			worldView.highlightGame(WorldView.GARIS_BATAS_BAWAH);
								}
								if (texts[texts.length-i-1].equals(WorldView.H_GARISAPOLLO)){
					    			worldView.highlightGame(WorldView.GARIS_ROKET_APOLLO);
								}
								if (texts[texts.length-i-1].equals(WorldView.H_GARISBULETAN)){
					    			worldView.highlightGame(WorldView.GARIS_BULETAN);
								}
							}
						});	
						worldView.post(new Runnable() {						
							public void run() {
								arrDialog[texts.length-i-1] = builder.create();
							}
						});									
					}
				}
				//tampilkan dialog yang pertama kali
				worldView.post(new Runnable() {				
					public void run() {
						arrDialog[0].show();
					}
				});			
			}
		}
	}
	
	public void Win(){				
//		if(isMultiplayer) {
//			winMultiplayer();//mekanisme win buat multiplayer agak beda
//			return;
//		}
//		//pass ke winneractivity siapa yang menang dan berapa jumlah kelereng yang didapat		
//		Bundle b = new Bundle();	
//		Intent intent = new Intent(WorldActivity.this,WInnerActivity.class);		
//		b.putString("namamenang", worldView.PlayerWinner());		
//		b.putInt("avatarid", Utility.playerHolder.getPlayerWithName(worldView.PlayerWinner()).avatar);
//		b.putInt("gundumenang", worldView.GunduPemenang());
//		intent.putExtras(b);
//		//update data pemenang		
//		if(WorldSetting.modegame == WorldSetting.APOLLO || WorldSetting.modegame == WorldSetting.BULETAN) {			
//			Utility.playerHolder.updatePlayerData(this,worldView.PlayerWinner(), worldView.GunduPemenang(), WorldSetting.taruhan, true);			
//		}else if(WorldSetting.modegame==WorldSetting.DUEL) {
//			//workaround buat duel(baca petunjuk updateplayerdata)			
//			Utility.playerHolder.updatePlayerData(this,worldView.PlayerWinner(), worldView.GunduPemenang() + 1, WorldSetting.taruhan, true);		
//		}
//			
//		//pass ke listrekap nama2 yang kalah dan jumlah gundu yang didapat
//		String[] arrNamaPlayer = worldView.LoserPlayer();
//		Player[] arrPlayer = new Player[worldView.LoserPlayer().length];		
//		ListRekap.jumGundu = worldView.LoserGundu();
//		for(int i=0;i<worldView.LoserPlayer().length;i++) {
//			arrPlayer[i] = Utility.playerHolder.getPlayerWithName(arrNamaPlayer[i]);
//			//update data yang kalah(jika bukan dalam mode multiplayer)	
//			Utility.playerHolder.updatePlayerData(this,arrNamaPlayer[i],ListRekap.jumGundu[i],WorldSetting.taruhan,false);
//		}		
//		ListRekap.paraPemain = arrPlayer;
//		startActivity(intent);		
//		overridePendingTransition(R.anim.fade, R.anim.hold);
//		worldView.thread.setRunning(false);
//		GameSound.changeMusicFromGameToMain(this);//ganti musik jadi musik main menu		
//		this.finish();
	}
	
	public void winMultiplayer() {
//		Intent data = new Intent();
//		//data player pemenang
//		data.putExtra(EXTRA_WINNER_NAME,worldView.PlayerWinner());
//		data.putExtra(EXTRA_WINNER_GUNDU, worldView.GunduPemenang());		
//		//data player yang kalah
//		data.putExtra(EXTRA_LOSERS_NAMES, worldView.LoserPlayer());
//		data.putExtra(EXTRA_LOSERS_GUNDU,worldView.LoserGundu());
//		this.setResult(Activity.RESULT_OK, data);
//		
//		worldView.thread.setRunning(false);
//		GameSound.changeMusicFromGameToMain(this);//ganti musik jadi musik main menu
//		this.finish();
	}
}
